﻿using Estsh.Core.Controllers;
using Estsh.Core.IServices;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Model.Result;
using Estsh.Core.Models;
using Estsh.Core.Services;
using Estsh.Core.Util;
using Estsh.Core.Wms.IServices;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json.Linq;
using System.Collections;
using System.Text.Json;

namespace Estsh.Core.Web.Plugin.Wms.Controllers
{
    [Route("wms/[controller]")]
    [ApiController]
    public class NcConcessionController : WmsBaseController
    {
        private INcConcessionService service;
        private IMoveOrderService moveOrderService;

        private readonly IMemoryCache memoryCache;

        public NcConcessionController(INcConcessionService _service,IMoveOrderService _moveOrderService, IMemoryCache _memoryCache)
        {
            this.service = _service;
            this.moveOrderService = _moveOrderService;
            this.memoryCache = _memoryCache;
        }

        /// <summary>
        /// NC让步接收订单列表
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetNcConcessionOrderList")]
        public IActionResult GetNcConcessionOrderList(Newtonsoft.Json.Linq.JObject jobj)
        {

            WmsResponseResult result = new WmsResponseResult();
            try
            {
				string orderNo = jobj["orderNo"].ToString();
				List<WmsMoveHeader> data = moveOrderService.GetMoveOrderList((int)WmsEnumUtil.MoveOrderType.NC_CONCESSION, orderNo);
                if (data.Count > 0)
                {
                    result.Msg = "OK";
					result.Success = true;
					result.Data = data;
				}
                else
                {
					result.Success = true;
					result.Msg = "NO";
				}
                
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// NC让步接收单明细列表
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetNcConcessionDetailList")]
        public IActionResult GetNcConcessionDetailList(JObject requestInfo)
        {
            string orderNo = requestInfo["orderNo"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                List<WmsMoveDetail> data = moveOrderService.GetMoveDetailList(orderNo);
                result.Success = true;
                result.Data = data;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 检查库位是否可操作
        /// </summary>
        /// <param name="locateName"></param>
        /// <returns></returns>
        [HttpPost("CheckLocate")]
        public IActionResult CheckLocateInfo(JObject requestInfo)
        {
            string locateName = requestInfo["locateName"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.CheckLocateInfo(locateName);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);

        }


        /// <summary>
        /// 处理扫描的箱条码
        /// </summary>
        /// <param name="cartonNo"></param>
        /// <param name="orderNo"></param>
        /// <param name="loginId"></param>
        /// <returns></returns>
        [HttpPost("DoCanton")]
        public IActionResult DoCanton(JObject requestInfo)
        {
            string cartonNo = requestInfo["cartonNo"].ToString();
            string locateName = requestInfo["locateName"].ToString();
            string orderNo = requestInfo["orderNo"].ToString();
            string loginId = requestInfo["loginId"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.DoCanton(cartonNo, locateName, orderNo, loginId);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 提交单据
        /// </summary>
        /// <param name="orderNo"></param>
        /// <param name="loginId"></param>
        /// <returns></returns>
        [HttpPost("SubmitOrder")]
        public IActionResult SubmitOrder(JObject requestInfo)
        {
            string orderNo = requestInfo["orderNo"].ToString();
            string loginId = requestInfo["loginId"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.SubmitOrder(orderNo, loginId);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }





    }
}
